package com.haipumi.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.haipumi.beans.haipumi_activity;
import com.haipumi.beans.haipumi_skin;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.IBackCustBiz;
import com.haipumi.dao.IBackCustDao;
import com.haipumi.mapper.BackCustMapper;
import com.haipumi.utils.Encrypt;


@Service("backCustBiz")
public class BackCustBizImpl implements IBackCustBiz{
	
	
	@Autowired
	BackCustMapper backCustMapper;
	
	@Override
	public List<haipumi_user> findCust(HttpServletRequest request) {
		String seacheroptions=null;
		seacheroptions=request.getParameter("searchOptions");
		String seacherType=request.getParameter("seacherType");
		List<haipumi_user>userlist=null;
		if(seacheroptions==""){
			//说明是初始化时候
			userlist=backCustMapper.findAllUser();
		}else{
			//查找的时候
			//userlist=backCustMapper.seacherUser(request, seacherType);
			if("mohu".equals(seacherType)){
				userlist=backCustMapper.seacherUser_mohu(seacheroptions);
			}else if("jingque".equals(seacherType)){
				userlist=backCustMapper.seacherUser_jingque(seacheroptions);
			}else if("qq".equals(seacherType)){
				userlist=backCustMapper.seacherUser_qq(seacheroptions);
			}else if("uid".equals(seacherType)){
				userlist=backCustMapper.seacherUser_uid(Integer.parseInt(seacheroptions));
			}
		}
		return userlist;
	}

	@Override
	public int findusercount(HttpServletRequest request) {
		String seacherType=null;
		seacherType=request.getParameter("seacherType");
		String seacheroptions=request.getParameter("searchOptions");
		int r=0;
		if(seacheroptions==""){
			r=backCustMapper.findCount();
		}else{
			//r=backCustMapper.findCount(request, seacherType);
			if("mohu".equals(seacherType)){
				r=backCustMapper.findCount_mohu(seacherType);
			}else if("jingque".equals(seacherType)){
				r=backCustMapper.findCount_jingque(seacherType);
			}else if("qq".equals(seacherType)){
				r=backCustMapper.findCount_qq(seacherType);
			}else if("uid".equals(seacherType)){
				r=backCustMapper.findCount_uid(Integer.parseInt(seacherType));
			}
		}
		return r;
	}

	@Override
	public haipumi_user insertCust(haipumi_user user) {
		String md5pwd=Encrypt.md5(user.getUpwd());
		user.setUpwd(md5pwd);
		int result=backCustMapper.insertCust(user);
		if(result>0){
			user.setUid(Integer.toString(result));
		}
		return null;
	}

	@Override
	public haipumi_user updateUserImage(haipumi_user user) {
		Map<String,Object> map=new HashMap<String ,Object>();
		map.put("uimage", user.getUimage());
		map.put("uid",Integer.parseInt(user.getUid()));
		int result=backCustMapper.updateUserImage(map);
		haipumi_user backuser=null;
		if(result>0){
			//成功了
			backuser=backCustMapper.findUser(Integer.parseInt(user.getUid()));
			backuser.setUimage(user.getUimage());
		}else{
			//失败了，返回原先的值
			backuser=backCustMapper.findUser(Integer.parseInt(user.getUid()));
		}
		return backuser;
	}

	@Override
	public haipumi_user updateUser(haipumi_user user) {
		String oldpwd=backCustMapper.findUser(Integer.parseInt(user.getUid())).getUpwd();
		String newpwd=user.getUpwd();
		String md5pwd=Encrypt.md5(user.getUpwd());
		if(!oldpwd.equals(newpwd)){
			user.setUpwd(md5pwd);
		}
		Map<String ,Object> map=new HashMap<String ,Object>();
		map.put("ulogon", user.getUlogon());
		map.put("upwd", user.getUpwd());
		map.put("ualais",user.getUalais());
		map.put("uwealth", Integer.parseInt(user.getUwealth()));
		map.put("urank", Integer.parseInt(user.getUrank()));
		map.put("uvip", Integer.parseInt(user.getUvip()));
		map.put("uglory", user.getUglory());
		map.put("uid", Integer.parseInt(user.getUid()));
		
		int result=backCustMapper.updateUser(map);
		if(result>0){
			return user;
		}
		return backCustMapper.findUser(Integer.parseInt(user.getUid()));
	}

	@Override
	public haipumi_skin updateSimage(haipumi_skin skin) {
		Map<String ,Object> map=new HashMap<String ,Object>();
		map.put("simage", skin.getSimage());
		map.put("sid", Integer.parseInt(skin.getSid()));
		int result=backCustMapper.updatesimage(map);
		haipumi_skin newskin=null;
		if(result>0){
			//成功了
			newskin=backCustMapper.findskin(Integer.parseInt(skin.getSid()));
			newskin.setSimage(skin.getSimage());
		}else{
			//失败了，返回原先的值
			newskin=backCustMapper.findskin(Integer.parseInt(skin.getSid()));
		}
		return newskin;
	}

	@Override
	public haipumi_activity acphoto(haipumi_activity activity) {
		int result=backCustMapper.acphoto(activity);
		haipumi_activity newactivity=null;
		if(result>0){
			//成功了
			newactivity=backCustMapper.findactivity(Integer.parseInt(activity.getAcid()));
			newactivity.setAcphoto(activity.getAcphoto());
		}else{
			//失败了，返回原先的值
			newactivity=backCustMapper.findactivity(Integer.parseInt(activity.getAcid()));
		}
		return newactivity;
	}

}
